package com.learning.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.learning.constant.MessageConstant;
import com.learning.dto.LoginDTO;
import com.learning.entity.Student;
import com.learning.exceptiom.AccountNotFoundException;
import com.learning.exceptiom.PasswordErrorException;
import com.learning.mapper.StudentMapper;
import com.learning.service.IStudentService;

import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

	@Override
	public Student login(LoginDTO loginDTO) {
		String username = loginDTO.getUsername();
		String password = loginDTO.getPassword();

		Student student = lambdaQuery().eq(Student::getUsername, username).one();

		//2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
		if (student == null) {
			//账号不存在
			throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
		}

		//密码比对
		password = DigestUtils.md5DigestAsHex(password.getBytes()); //密码进行md5加密后比对
		if (!password.equals(student.getPassword())) {
			//密码错误
			throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
		}

		return student;
	}
}
